package com.example.zzj.ziwuyundemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zzj.ziwuyundemo.activity.LoginActivity;
import com.example.zzj.ziwuyundemo.adapter.MainAdapter;
import com.example.zzj.ziwuyundemo.base.BaseActivity;
import com.example.zzj.ziwuyundemo.fragment.AnswerFragment;
import com.example.zzj.ziwuyundemo.fragment.HomeFragment;
import com.example.zzj.ziwuyundemo.fragment.InquiryFragment;
import com.example.zzj.ziwuyundemo.fragment.MessageFragment;
import com.example.zzj.ziwuyundemo.util.ControlScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends BaseActivity implements View.OnClickListener {


    @InjectView(R.id.viewPager)
    ControlScrollViewPager viewPager;
    @InjectView(R.id.tabLayout)
    TabLayout tabLayout;
    @InjectView(R.id.img_login)
    CircleImageView imgLogin;
    @InjectView(R.id.name_home)
    TextView nameHome;
    @InjectView(R.id.name_answer)
    TextView nameAnswer;
    @InjectView(R.id.name_inquiry)
    TextView nameInquiry;
    @InjectView(R.id.img_info)
    ImageView imgInfo;
    @InjectView(R.id.activity_main)
    LinearLayout activityMain;


    private TabLayout.Tab one;
    private TabLayout.Tab two;
    private TabLayout.Tab three;
    private TabLayout.Tab four;

    private List<Fragment> fragmentList;
    private MainAdapter mainAdapter;
    private List<String> titles;
    private int[] imgs;
    private int[] img_select;
    private int[] img_noSelect;
    private List<View> viewList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        initView();
        initData();
    }

    private void initView() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new AnswerFragment());
        fragmentList.add(new InquiryFragment());
        fragmentList.add(new MessageFragment());

        titles = new ArrayList<>();
        titles.add("主页");
        titles.add("响应");
        titles.add("排队查询");
        titles.add("个人信息");

        viewList = new ArrayList<>();

        imgs = new int[]{R.mipmap.home_red, R.mipmap.answer_glay, R.mipmap.inquiry_glay, R.mipmap.message_glay};
        img_select = new int[]{R.mipmap.home_red, R.mipmap.answer_red, R.mipmap.inquiry_red, R.mipmap.message_red};
        img_noSelect = new int[]{R.mipmap.home_glay, R.mipmap.answer_glay, R.mipmap.inquiry_glay, R.mipmap.message_glay};

        for (int i = 0; i < 4; i++) {
            View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_item, null);
            TextView tabText = (TextView) v.findViewById(R.id.tab_text);
            tabText.setText(titles.get(i));
            ImageView tabImg = (ImageView) v.findViewById(R.id.tab_img);
            tabImg.setImageResource(imgs[i]);
            viewList.add(v);

            imgLogin.setOnClickListener(this);
        }

        mainAdapter = new MainAdapter(getSupportFragmentManager(), fragmentList, titles, viewList);
        viewPager.setAdapter(mainAdapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(mainAdapter.getTabView(i));
            }
        }
        ((TextView) (tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab_text))).setTextColor(getResources().getColor(R.color.red));
        imgLogin.setVisibility(View.VISIBLE);
        nameHome.setVisibility(View.VISIBLE);
        nameAnswer.setVisibility(View.GONE);
        nameInquiry.setVisibility(View.GONE);
        imgInfo.setVisibility(View.VISIBLE);
    }

    private void initData() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                ((TextView) (tab.getCustomView().findViewById(R.id.tab_text))).setTextColor(getResources().getColor(R.color.red));
                ((ImageView) (tab.getCustomView().findViewById(R.id.tab_img))).setImageResource(img_select[tab.getPosition()]);
                if (tab == tabLayout.getTabAt(0)) {
                    imgLogin.setVisibility(View.VISIBLE);
                    nameHome.setVisibility(View.VISIBLE);
                    nameAnswer.setVisibility(View.GONE);
                    nameInquiry.setVisibility(View.GONE);
                    imgInfo.setVisibility(View.VISIBLE);
                } else if (tab == tabLayout.getTabAt(1)) {
                    imgLogin.setVisibility(View.GONE);
                    nameHome.setVisibility(View.GONE);
                    nameAnswer.setVisibility(View.VISIBLE);
                    nameInquiry.setVisibility(View.GONE);
                    imgInfo.setVisibility(View.VISIBLE);
                } else if (tab == tabLayout.getTabAt(2)) {
                    imgLogin.setVisibility(View.GONE);
                    nameHome.setVisibility(View.GONE);
                    nameAnswer.setVisibility(View.GONE);
                    nameInquiry.setVisibility(View.VISIBLE);
                    imgInfo.setVisibility(View.GONE);
                } else if (tab == tabLayout.getTabAt(3)) {
                    imgLogin.setVisibility(View.GONE);
                    nameHome.setVisibility(View.GONE);
                    nameAnswer.setVisibility(View.GONE);
                    nameInquiry.setVisibility(View.GONE);
                    imgInfo.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ((TextView) (tab.getCustomView().findViewById(R.id.tab_text))).setTextColor(getResources().getColor(R.color.blank_color));
                ((ImageView) (tab.getCustomView().findViewById(R.id.tab_img))).setImageResource(img_noSelect[tab.getPosition()]);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_login:
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                break;
        }
    }

}
